package com.springboot.wisdom.auth.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.springboot.wisdom.common.common.ResultUtil;
import com.springboot.wisdom.common.common.bean.MyResult;
import com.springboot.wisdom.common.common.constant.LoginTypeEnum;
import com.springboot.wisdom.serviceApi.user.model.dto.UserInfoDTO;
import com.springboot.wisdom.serviceApi.user.model.vo.LoginDTO;
import com.springboot.wisdom.serviceApi.user.model.vo.LoginUserVO;
import com.springboot.wisdom.serviceApi.user.model.vo.RegisterUserVO;
import com.springboot.wisdom.serviceApi.user.model.vo.UserTokenInfo;
import com.springboot.wisdom.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * @author ReStartLin
 * @date 2021/9/15 16:48
 * @description: 功能描述:
 */
@RestController
@RequestMapping(value = "/auth", produces = {"application/json;charset=UTF-8"})
//@AllArgsConstructor
@Api(tags = "授权管理")
@Slf4j
public class AuthController {

    @Resource
    private UserService userService;

    @Value("${file.windowsLocal.path}")
    private String path;



    @ApiOperation(value = "登录", notes = "用于区分登录方式【暂只支持用户名密码登录】")
    @PostMapping("/login")
    public MyResult<UserTokenInfo> login(@RequestBody @Valid LoginDTO loginUserVO, HttpServletRequest request) {

        System.out.println("path = " + path);

        LoginTypeEnum loginType = loginUserVO.getLoginType();
        switch (loginType) {
            // 用户名密码登录。
            case USER_PASS:
                LoginUserVO loginData = loginUserVO.getLoginData();
                if (loginData == null) {
                    return ResultUtil.error("请填写登录信息");
                }
                MyResult<UserTokenInfo> result = userService.userPassLogin(loginData, request);
                log.info("登录返回信息：{}", result.toString());
                return result;
            default:
                return ResultUtil.error("未知登录方式！");
        }
    }

    @ApiOperation(value = "注册", notes = "")
    @PostMapping("/register")
    public MyResult<UserInfoDTO> register(@RequestBody @Valid RegisterUserVO registerUserVO, HttpServletRequest request) {
        return userService.register(registerUserVO, request);
    }

    @ApiOperation("退出")
    @GetMapping("/logout")
    public MyResult logout(HttpServletRequest request) {
        return userService.logout(request);
    }

    @ApiOperation("获取公钥")
    @GetMapping("/public-key")
    public MyResult getPublicKey() {
//        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
//        RSAKey key = new RSAKey.Builder(publicKey).build();
//        JSONObject jsonObject = new JWKSet(key).toJSONObject();
//        return ResultUtil.success(jsonObject);
        return null;
    }

    @ApiOperation("检查是否登录")
    @GetMapping("/check_token")
    public MyResult checkToken() {
        String tokenValue = StpUtil.getTokenValue();
//        boolean verify = JWT.of(tokenValue).setSigner(jwtSigner).verify();
//        return ResultUtil.success(verify);
        return ResultUtil.success(true);
    }

 /*   @ApiOperation("ras公钥加密")
    @GetMapping("/rsa")
    public MyResult checkToken(@RequestParam String rsa, @RequestParam String psw) {
        String encrypt = SaSecureUtil.rsaEncryptByPublic(rsa, psw);
        return ResultUtil.success(encrypt);
    }*/
}
